const {ccclass, property} = cc._decorator;
import { PLAYER_TYPE } from "../Core/AppDef";
import { EVT_CREATE_PLAYER, EVT_PLAYER_HIT, EVT_PLAYER_SET_P, EVT_PLAYER_SET_WEAPON_P } from "../Core/AppEvent";
import Player from "../Player";
import { addListener } from "./EventManager";


@ccclass
export default class PlayerManager extends cc.Component {
    @property(cc.Prefab)
    playerPrefab: cc.Prefab = null;
    @property(cc.Prefab)
    enemyPrefab: cc.Prefab = null;
    @property(cc.Node)
    roleLayer: cc.Node = null;

    player: cc.Node = null;
    tempJoyStick = null;

    playerList : cc.Node[][] = [[],[]];
    collectPlayerPool : cc.Node[][] = [[],[]];
    maxNum = 10;

    onLoad () {
        addListener(EVT_PLAYER_SET_P, this.playerSetP, this);
        addListener(EVT_PLAYER_SET_WEAPON_P, this.playerSetWeaponP, this);
        addListener(EVT_CREATE_PLAYER, this.createPlayer, this);
        addListener(EVT_PLAYER_HIT, this.PlayerHit, this);

    }

    createPlayer (event: cc.Event.EventCustom) {
        let data = event.getUserData();
        let player = this.checkCollectList(data.playerType); 
        player.position = cc.v3(data.posx, data.posy, 0);
        player.name = "player.id=" + this.playerList[data.playerType].length + " playerType=" + data.playerType ;
        player.getComponent(Player).init(data.playerType, data.weapontype, '');
        this.roleLayer.addChild(player);
        this.playerList[data.playerType].push(player);
    }
    
    checkCollectList(playerType : PLAYER_TYPE){
        let length = this.collectPlayerPool[playerType].length
        if(length > 0){
            let node = this.collectPlayerPool[playerType][length-1];
            this.collectPlayerPool[playerType].pop();
            return node
        }else{
            if(playerType == PLAYER_TYPE.My){
                this.player = cc.instantiate(this.playerPrefab);
                return this.player;
            }else{
                return cc.instantiate(this.enemyPrefab);
            }
        }
    }

    collectPlayer(player){
        if(this.collectPlayerPool[player.playerType].length >= this.maxNum){
            player.destroy();
        }else{
            player.active = false;
            this.collectPlayerPool[player.playerType].push(player);
        }
    }
    
    playerSetP (event: cc.Event.EventCustom) {
        let data = event.getUserData();
        if(data && data.p){
            this.player.getComponent(Player).setP(data.p);
        }
    }

    playerSetWeaponP (event: cc.Event.EventCustom) {
        let data = event.getUserData();
        this.player.getComponent(Player).setWeaponP(data);
    }

    PlayerHit(event: cc.Event.EventCustom) {
        let data = event.getUserData();
        let isFind = false;
        for(let i=0; i<this.playerList.length; i++){
            for(let j=0; j<this.playerList[i].length; j++){
                let item = this.playerList[i][j];
                if(item.name === data.playerName){
                    let scrtpt = item.getComponent(Player)
                    scrtpt.setHpReduce(10);
                    if(scrtpt.getPlayerHp() <= 0){
                        this.collectPlayer(item);
                        isFind = true;
                        if (j !== -1) {
                            this.playerList.splice(j, 1);
                        }
                        break;
                    }
                }
            }
            if(isFind){ break; }
        }
    }


}
